package com.boarsoft.boar.code.mac.flow.generator;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.boarsoft.boar.code.entity.ProHandlerInfo;
import com.boarsoft.boar.code.mac.flow.biz.ProcessHandlerInfoBiz;
import com.boarsoft.boar.code.mac.flow.generator.method.Field;
import com.boarsoft.boar.code.mac.flow.generator.method.Method;

@Component
public class ProcessHandlerGenerator {

    @Autowired
    private ProcessHandlerInfoBiz processHandlerInfoBiz;

/*    public static void main(String[] args) throws IOException {
        //1.handler全名
        String absoluteHandlerName="com.scrcu.core.indv.mac.handler.OpenAccountProcessHandler";
        //2.目标组件bean类型
        String target="com.boar.component.Component";
        //3.目标组件方法
        String mets="process(demo.scrcu.core.intd.bean.User,demo.scrcu.core.intd.bean.User)";
        generate(absoluteHandlerName,target,mets);
    }*/

    public String generate(String code, String appId) throws IOException {
       ProHandlerInfo proHandlerInfo = processHandlerInfoBiz.get(code, appId);
       return generate(proHandlerInfo.getCode(),proHandlerInfo.getFaceCode(),proHandlerInfo.getMethodCode());
    }

    protected String generate(String absoluteName, String target, String mets) throws IOException {
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        //封装模板数据
        Map<String, Object> map = new HashMap<>();
        String packageNm=absoluteName.substring(0,absoluteName.lastIndexOf("."));
        String classNm=absoluteName.substring(absoluteName.lastIndexOf(".")+1);
        Method method=new Method();
        method.setMethodName(mets.substring(0,mets.indexOf("(")));
        mets=mets.substring(mets.indexOf("(")).replaceAll("\\(","").replaceAll("\\)","");
        String[] parmTypes=mets.split(",");
        method.setParamList(new Field[parmTypes.length]);
        for(int i=0;i<parmTypes.length;i++){
            Field f = new Field();
            f.setName("f"+i);
            f.setType(parmTypes[i]);
            method.getParamList()[i] = f;
        }

        //1.包路径
        map.put("package", packageNm);
        //2.import
        map.put("target", target);
        //3.processHandler类名
        map.put("className", classNm);
        //4.组件注入
        map.put("targetClass", target.substring(target.lastIndexOf(".")+1));
        map.put("targetInstance", target.substring(target.lastIndexOf(".")+1).toLowerCase());
        //5.组件调用生成
        map.put("method",method.getMethodName());
        String params="";
        for(int i=0;i<method.getParamList().length;i++){
            Field field=method.getParamList()[i];
            params=params.concat("("+field.getType()+")args["+i+"],");
        }
        map.put("params",params.substring(0,params.length()-1));

        VelocityContext context = new VelocityContext(map);
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate("template/Handler.java.vm", "UTF-8");
        tpl.merge(context, sw);//将数据context合并入模板tpl中，获得的数据写入sw

        String path="D:\\"+classNm+".java";
        OutputStream ouputStream = new FileOutputStream(path);
        IOUtils.write(sw.toString(), ouputStream, "UTF-8");
        IOUtils.closeQuietly(sw);
        return sw.toString();
    }

}
